<?php
class Productfront_Model {

	public $con;
	public function __construct(){
		$this->con =  mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
		if(mysqli_connect_error())
		{
			echo "Failed to connect to MySQL". mysqli_connect_error();exit();
		}	
	}

	public function getListsPro() {
		$query = "SELECT * FROM product ";
		$result = mysqli_query($this->con,$query);
		//var_dump($result); exit();
		return $result;
	}
	
	public function getAPro($id) {
		$query = "SELECT p.id, p.title, p.content, p.idCatPro,p.image, p.alias, cp.nameCat
		FROM product AS p, catproduct AS cp, product_cat AS pc
		WHERE cp.id = pc.idCat
		AND pc.idproduct = p.id
		AND p.id ='$id'";
		$result = mysqli_query($this->con,$query);
		//var_dump($result); exit();
		return $result;
	}
	public function getListsProFollowCat($cat) {
		$query = "SELECT pc.idproduct, p.title, p.image, p.content, p.idCatPro
					FROM product_cat AS pc, catproduct AS ca, product AS p
					WHERE ca.nameCat ='$cat'
					AND p.id = pc.idproduct
					AND ca.id = pc.idCat";
		$result = mysqli_query($this->con,$query);
		//var_dump($result); exit();
		return $result;
	}
	public function getCatProIdProduct($id) {
		$query = "SELECT pc.idproduct, p.title, p.image, p.content, p.idCatPro, ca.nameCat
			FROM product_cat AS pc, catproduct AS ca, product AS p
			WHERE p.id =  '$id'
			AND p.id = pc.idproduct
			AND ca.id = pc.idCat";
		$result = mysqli_query($this->con,$query);
		//var_dump($result); exit();
		return $result;
	}
	public function getAllProForACat($cat) {
		$query = "SELECT pc.id,pc.idproduct, p.title, p.image, p.content, p.idCatPro,ca.nameCat
					FROM product_cat AS pc, catproduct AS ca, product AS p
					WHERE ca.nameCat ='$cat'
					AND p.id = pc.idproduct
					AND ca.id = pc.idCat";
		$result = mysqli_query($this->con,$query);
		//var_dump($result); exit();
		return $result;
	}
	public function getTittle($id) {
		$query = "SELECT title FROM product WHERE id='$id' ";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getId($title) {
		$query = "SELECT id FROM product WHERE title='$title' ";
		//exit($query);
		$result = mysqli_query($this->con,$query);
		//exit($result);
		return $result;
	}
	
	public function getImage($id) {
		$query = "SELECT image FROM product WHERE id='$id' ";
		$result = mysqli_query($this->con,$query);
		return $result;
	}
	public function getIdCat() {
		$query = "SELECT * FROM product_cat";
		//exit($query);
		$result = mysqli_query($this->con,$query) or die("MySQL error: " . mysqli_error($this->con) . "<hr>\nQuery: $query");  
		//printf ("Error: %s\n", mysqli_error($result));
		//exit();
		//var_dump($result); exit();
		//exit($result);
		return $result;
	}
	/* public function addPro($title,$content,$img,$idcat,$alias) {

		$query = "INSERT INTO product(title,image,content,idCatPro,alias) VALUES ('$title','$img','$content','$idcat','$alias')";
		exit($query);
		$result = mysqli_query($this->con,$query);

	}  */
	
	public function addPro($title,$content,$img,$alias) {

		$query = "INSERT INTO product(title,image,content,alias) VALUES ('$title','$img','$content','$alias')";
		//exit($query);
		$result = mysqli_query($this->con,$query);

	}
	public function addCat($Idproduct,$IdCat) {
		//exit($Idproduct);
		$query = "INSERT INTO product_cat(idproduct,idcat) VALUES ('$Idproduct','$IdCat')";
		//var_dump($query);
		$result = mysqli_query($this->con,$query);
		//exit('fvdv');

	}
	
	public function editProduct($id,$title,$content,$alias) {
		//exit('test');
		$query = "UPDATE product SET title='$title',content='$content',alias='$alias' WHERE id='$id'"; 
		$result = mysqli_query($this->con,$query) or die("MySQL error: " . mysqli_error($this->con) . "<hr>\nQuery: $query");
		//exit('khiem');
		//var_dump($result); exit();
		//return $result;
	}
	function deleteCatofProduct($id){
	//exit($id);
	$query = "DELETE FROM product_cat WHERE idproduct = '$id'";
	mysqli_query($this->con,$query);
}
	
	function deleteProduct($id){
	$query = "DELETE FROM product WHERE id = '$id'";
	mysqli_query($this->con,$query);
}
	function deleteProductCat($id){
	$query = "DELETE FROM product_cat WHERE idproduct = '$id'";
	mysqli_query($this->con,$query);
}
	public function getIdCatProduct($idProduct) {
		$query = "SELECT e1.idproduct, e1.idcat, v2.nameCat FROM product_cat AS e1, catproduct AS v2
			WHERE (e1.idcat = v2.id)
			AND (e1.idproduct ='$idProduct')";
		//exit($query);
		$result = mysqli_query($this->con,$query)  or die("MySQL error: " . mysqli_error($this->con) . "<hr>\nQuery: $query");
		return $result;
	}
}
?>